﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kelper.Infrastructure.Abstract;
using System.Web.Routing;
using Kelper.Infrastructure;

namespace Kelper.Controllers
{
    public abstract class BaseController : Controller
    {
        protected IAccountManager accounts;

        public int CurrentPage { get; set; }
        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }
        private int pageSize = 10;

        public BaseController() : this(null) { }

        public BaseController(IAccountManager accounts)
        {
            this.accounts = accounts;
        }

        [NonAction]
        public IEnumerable<T> Paginate<T>(IEnumerable<T> enumeration, int page)
        {
            return enumeration
                .Skip((page - 1) * PageSize)
                .Take(PageSize);
        }

        protected override void Execute(RequestContext requestContext)
        {
            if (accounts == null)
                accounts = new SessionAccountManager(requestContext.HttpContext.Session);
            base.Execute(requestContext);
        }
    }
}
